import Big from 'big.js'
import {
    getMoment
} from '@/utils/time.js'

const adminHomeStatistics=uniCloud.importObject(`admin-home-statistics`,{
    customUI:true
})

export async function getAmountAndUserStatistics(topMenus){
    const {myErrCode,data,errMsg }=await adminHomeStatistics.getAmountAndUserStatistics();
    if(myErrCode!==0){
        console.error(errMsg)
        return 
    }
    console.log(data)
    const { trade,user }=data

    topMenus.menusOne[0].value=trade.totalAmount
    topMenus.menusOne[0].list={
        ...topMenus.menusOne[0].list,
        progress:trade.total.length>0?100:0,
        num_text:trade.total.length
    }
    topMenus.menusOne[1].value=trade.successAmount
    topMenus.menusOne[1].list={
        ...topMenus.menusOne[1].list,
        progress:trade.success.length/trade.total.length*100,
        num_text:trade.success.length
    }
    topMenus.menusOne[2].value=trade.refundAmount
    topMenus.menusOne[2].list={
        ...topMenus.menusOne[2].list,
        progress:trade.refund.length/trade.total.length*100,
        num_text:trade.refund.length
    }

    topMenus.menusTwo[0].value=user.user
    topMenus.menusTwo[0].list={
        ...topMenus.menusTwo[0].list,
        progress:user.user/user.total*100,
        num_text:user.user
    }
    topMenus.menusTwo[1].value=user.merchant
    topMenus.menusTwo[1].list={
        ...topMenus.menusTwo[1].list,
        progress:user.merchant/user.total*100,
        num_text:user.merchant
    }
    topMenus.menusTwo[2].value=user.rider
    topMenus.menusTwo[2].list={
        ...topMenus.menusTwo[2].list,
        progress:user.rider/user.total*100,
        num_text:user.rider
    }
    return data
}

export async function getPlatformProfitStatistics(startMs,startTimeMS,endTimeMS,showDay){
    const {myErrCode,data,errMsg }=await adminHomeStatistics.getPlatformProfitStatistics(startTimeMS,endTimeMS);
    const options={
        legend: {},
        tooltip: {
            formatter:(item)=>{
                const {name,marker,seriesName,value} =item;
                return `${seriesName}\r\n${marker}${name} ${value}元`
            }
        },
        xAxis: { 
            type: 'category',
            data: [],
        },
        yAxis: {},
        series: [{ 
            type: 'bar',
            barWidth:'10px',
            name:`毛利润`,
            data:new Array(showDay).fill(0),
            itemStyle:{
                color:'#32d79f'
            }
        }, {
            type: 'bar',
            barWidth:'10px',
            name:`净利润`,
            data:new Array(showDay).fill(0),
            itemStyle:{
                color:'#713bdb'
            }
        }]
    }
    for(let i=showDay;i>0;i--){
        options.xAxis.data.push(getMoment(startMs - 1000*60*60*24*i).format(`MM-DD`))
    }
    if(myErrCode!==0){
        console.error(errMsg)
    }else{
        for(let key in data){
            const { 
                grossProfit_fen,    // 毛利 单位分
                netProfit_fen   //纯利 单位分
            }=data[key] 
            const index= options.xAxis.data.findIndex(it=>it==key);
            options.series[0].data[index]= +new Big(grossProfit_fen).div(100).toNumber();       //分转成元
            options.series[1].data[index]= +new Big(netProfit_fen).div(100).toNumber();       //分转成元
        }
    }
    return options
}

export async function getUserTrendStatistics(startMs,startTimeMS,endTimeMS,showDay){
    const {myErrCode,data,errMsg }=await adminHomeStatistics.getUserTrendStatistics(startTimeMS,endTimeMS);
    const options={
        legend: {},
        tooltip: {
            formatter:(item)=>{
                const {name,marker,seriesName,value} =item;
                return `${seriesName}\r\n${marker}${name} ${value}个`
            }
        },
        xAxis: { 
            type: 'category',
            data: [],
        },
        yAxis: {},
        series: [{ 
            type: 'bar',
            barWidth:'10px',
            name:`新增用户`,
            data: new Array(showDay).fill(0),
            itemStyle:{
                color:'#f95483'
            }
        }, {
            type: 'bar',
            barWidth:'10px',
            name:`新增商户`,
            data: new Array(showDay).fill(0),
            itemStyle:{
                color:'#cbb64d'
            }
        },{
            type: 'bar',
            barWidth:'10px',
            name:`新增骑手`,
            data: new Array(showDay).fill(0),
            itemStyle:{
                color:'#058373'
            }
        }]
    }
    for(let i=showDay;i>0;i--){
        options.xAxis.data.push(getMoment(startMs - 1000*60*60*24*i).format(`MM-DD`))
    }
    if(myErrCode!==0){
        console.error(errMsg)
    }else{
        for(let key in data){
            const { 
                merchant,     //新增商户
                rider,       //新增骑手
                user,       //新增用户
            }=data[key] 
            const index= options.xAxis.data.findIndex(it=>it==key);
            options.series[0].data[index]= user;      
            options.series[1].data[index]= merchant;
            options.series[2].data[index]= rider;
        }
    }
    return options
}

export async function getLatestOrderList(count=10){
    const {myErrCode,data,errMsg }=await adminHomeStatistics.getLatestOrderList(count);
    if(myErrCode!==0){
        console.error(errMsg)
        return []
    }
    return data.map(it=>{
        it.create_date_format=getMoment(it.create_date).format(`MM-DD HH:mm:ss`)
        if(it.moduleName===`runErrand`){
            it.platform_store_name=`快递代取`
            it.platform_product_name=`-`
        }else{
            it.platform_store_name=it.product_list[0].store_name
            it.platform_product_name=it.product_list[0].product_list.map(item=>item.product_name).join('、')
        }
        return it;
    })
}